package ru.maxmatveev.dyndao;

import org.hibernate.SessionFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;

import java.io.Serializable;
import java.util.List;

/**
 * Basic interface for Dynamic DAO.
 * @param <T> Entity type
 * @param <K> ID type
 * @author Max Matveev
 */
public interface DynamicDao<T, K extends Serializable> {

    /**
     * Get associated session factory.
     * @return SessionFactory
     */
    SessionFactory getSessionFactory();

    /**
     * Delete entity from persistent storage.
     * @param entity persistent entity to make transient
     * @return transient entity
     */
    T delete(T entity);

    /**
     * Get entity by it's ID.
     * @param id ID of entity to get
     * @return entity, or <code>null</code> if entity with such id is not found
     */
    T getById(K id);

    /**
     * Get all entities.
     * @return List with all entities of handled type
     */
    List<T> getAll();

    /**
     * Save transient entity in persistent storage.
     * If primary key is specified entity is saved with such ID, elsewhere ID is generated.
     * @param entity entity to save
     * @return persistent entity
     */
    T save(T entity);

    /**
     * Merge detached entity with session.
     * @param entity entiry to merge
     * @return merged persistent entity
     */
    T merge(T entity);

    Criterion getCriterion(Restriction restriction, Criteria criteria);
}
